package com.segersten.othello.service.akka.game.dao;

import java.util.HashMap;
import java.util.Map;

import com.segersten.othello.service.akka.Auth;
import com.segersten.othello.service.akka.Player;

public class InMemoryGameDao implements GameDao
{
    private Map<Auth, Player> players = new HashMap<Auth, Player>();
    private Player playerBlack;
    private Player playerWhite;

    @Override
    public void updateSession(Auth replacedSession, Player player)
    {
        players.remove(replacedSession);
        players.put(player.getAuth(), player);
        if (this.playerWhite.getAuth().equals(replacedSession)) {
            playerWhite=player;
        } else if (this.playerBlack.getAuth().equals(replacedSession)) {
            playerBlack=player;
        } else {
            throw new IllegalStateException("Could not login player " + player + " could not match " + players);
        }
    }

    @Override
    public Auth getPlayerBlackAuth()
    {
        return this.playerBlack.getAuth();
    }

    @Override
    public Auth getPlayerWhiteAuth()
    {
        return this.playerWhite.getAuth();
    }

    @Override
    public boolean isAuthorized(Auth auth)
    {
        return  players.containsKey(auth) == false;
    }

    @Override
    public void setPlayerBlack(Player player)
    {
        playerBlack = player;
        this.players.put(player.getAuth(), player);
    }
    
    @Override
    public void setPlayerWhite(Player player)
    {
        playerWhite = player;
        this.players.put(player.getAuth(), player);
    }

}
